package com.ofsoft.weixin.mp.api;

import java.util.List;

/**
 * 客服接口
 * 
 * @author OF
 * @date 2018年07月21日
 */
public class CustomServiceApi extends ApiBase {
	private static String addAccountUrl = "https://api.weixin.qq.com/customservice/kfaccount/add?access_token=";

	/**
	 * 添加客服帐号
	 * 
	 * <p>
	 * 每个公众号最多添加10个客服账号
	 * </p>
	 * 
	 * @param account
	 *            客服账号 账号前缀@公众号微信号
	 * @param nickname
	 *            客服名称 最长6个汉字或12个英文字符
	 * @param password
	 *            密码 客服账号登录密码，格式为密码明文的32位加密MD5值
	 * @return ApiResponse
	 */
	public static ApiResponse addAccount(String account, String nickname,
			String password) {
		ApiRequest request = new ApiRequest();
		request.set("kf_account", account);
		request.set("nickname", nickname);
		request.set("password", password);
		return sendPost(addAccountUrl + AccessTokenConfig.getAccessToken(),
				request);
	}

	private static String updateAccountUrl = "https://api.weixin.qq.com/customservice/kfaccount/update?access_token=";

	/**
	 * 修改客服帐号
	 * 
	 * @param account
	 *            客服账号 账号前缀@公众号微信号
	 * @param nickname
	 *            客服名称 最长6个汉字或12个英文字符
	 * @param password
	 *            密码 客服账号登录密码，格式为密码明文的32位加密MD5值
	 * @return ApiResponse
	 */
	public static ApiResponse updateAccount(String account, String nickname,
			String password) {
		ApiRequest request = new ApiRequest();
		request.set("kf_account", account);
		request.set("nickname", nickname);
		request.set("password", password);
		return sendPost(updateAccountUrl + AccessTokenConfig.getAccessToken(),
				request);
	}

	private static String delAccountUrl = "https://api.weixin.qq.com/customservice/kfaccount/del?access_token=";

	/**
	 * 删除客服帐号
	 * 
	 * @param account
	 *            客服账号 账号前缀@公众号微信号
	 * @return ApiResponse
	 */
	public static ApiResponse delAccount(String account) {
		ApiRequest request = new ApiRequest();
		request.set("kf_account", account);
		return sendPost(delAccountUrl + AccessTokenConfig.getAccessToken(),
				request);
	}

	private static String getListUrl = "https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token=";

	/**
	 * 获取所有客服账号
	 * 
	 * @return ApiResponse
	 */
	public static ApiResponse getList() {
		return sendGet(getListUrl + AccessTokenConfig.getAccessToken());
	}

	private static String sendMessageUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=";

	/**
	 * 发送文本消息
	 * 
	 * @param touser
	 *            open_id
	 * @param text
	 *            内容
	 * @return ApiResponse
	 */
	public static ApiResponse sendText(String touser, String text) {
		ApiRequest request = new ApiRequest();
		request.set("touser", touser);
		request.set("msgtype", "text");
		request.set("text", ApiRequest.create("content", text));
		return sendPost(sendMessageUrl + AccessTokenConfig.getAccessToken(),
				request);
	}

	/**
	 * 发送图片消息
	 * 
	 * @param touser
	 *            open_id
	 * @param mediaId
	 *            发送的图片/语音/视频/图文消息（点击跳转到图文消息页）的媒体ID
	 * @return ApiResponse
	 */
	public static ApiResponse sendImage(String touser, String mediaId) {
		ApiRequest request = new ApiRequest();
		request.set("touser", touser);
		request.set("msgtype", "image");
		request.set("image", ApiRequest.create("media_id", mediaId));
		return sendPost(sendMessageUrl + AccessTokenConfig.getAccessToken(),
				request);
	}

	/**
	 * 发送语音消息
	 * 
	 * @param touser
	 *            open_id
	 * @param mediaId
	 *            发送的图片/语音/视频/图文消息（点击跳转到图文消息页）的媒体ID
	 * @return ApiResponse
	 */
	public static ApiResponse sendVoice(String touser, String mediaId) {
		ApiRequest request = new ApiRequest();
		request.set("touser", touser);
		request.set("msgtype", "voice");
		request.set("voice", ApiRequest.create("media_id", mediaId));
		return sendPost(sendMessageUrl + AccessTokenConfig.getAccessToken(),
				request);
	}

	/**
	 * 发送视频消息
	 * 
	 * @param touser
	 *            open_id
	 * @param mediaId
	 *            发送的图片/语音/视频/图文消息（点击跳转到图文消息页）的媒体ID
	 * @param thumbMediaId
	 *            缩略图/小程序卡片图片的媒体ID，小程序卡片图片建议大小为520*416
	 * @param title
	 *            图文消息/视频消息/音乐消息/小程序卡片的标题
	 * @param description
	 *            图文消息/视频消息/音乐消息的描述
	 * @return ApiResponse
	 */
	public static ApiResponse sendVideo(String touser, String mediaId,
			String thumbMediaId, String title, String description) {
		ApiRequest request = new ApiRequest();
		request.set("touser", touser);
		request.set("msgtype", "video");
		request.set(
				"video",
				ApiRequest.create("media_id", mediaId)
						.set("thumb_media_id", thumbMediaId)
						.set("title", title).set("description", description));
		return sendPost(sendMessageUrl + AccessTokenConfig.getAccessToken(),
				request);
	}

	/**
	 * 发送音乐消息
	 * 
	 * @param touser
	 *            open_id
	 * @param mediaId
	 *            发送的图片/语音/视频/图文消息（点击跳转到图文消息页）的媒体ID
	 * @param thumbMediaId
	 *            缩略图
	 * @param musicurl
	 *            音乐链接
	 * @param hqmusicurl
	 *            高品质音乐链接，wifi环境优先使用该链接播放音乐
	 * @return ApiResponse
	 */
	public static ApiResponse sendMusic(String touser, String mediaId,
			String thumbMediaId, String musicurl, String hqmusicurl) {
		ApiRequest request = new ApiRequest();
		request.set("touser", touser);
		request.set("msgtype", "music");
		request.set(
				"music",
				ApiRequest.create("media_id", mediaId)
						.set("thumb_media_id", thumbMediaId)
						.set("musicurl", musicurl)
						.set("hqmusicurl", hqmusicurl));
		return sendPost(sendMessageUrl + AccessTokenConfig.getAccessToken(),
				request);
	}

	/**
	 * 发送图文消息（点击跳转到图文消息页面）
	 * 
	 * <pre>
	 * 图文消息条数限制在8条以内，注意，如果图文数超过8，则将会无响应。
	 * </pre>
	 * 
	 * @param touser
	 *            open_id
	 * @param mediaId
	 *            发送的图片/语音/视频/图文消息（点击跳转到图文消息页）的媒体ID
	 * @return ApiResponse
	 */
	public static ApiResponse sendMpnews(String touser, String mediaId) {
		ApiRequest request = new ApiRequest();
		request.set("touser", touser);
		request.set("msgtype", "mpnews");
		request.set("mpnews", ApiRequest.create("media_id", mediaId));
		return sendPost(sendMessageUrl + AccessTokenConfig.getAccessToken(),
				request);
	}

	public static class Articles {
		private String title;
		private String description;
		private String url;
		private String picurl;

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getDescription() {
			return description;
		}

		public void setDescription(String description) {
			this.description = description;
		}

		public String getUrl() {
			return url;
		}

		public void setUrl(String url) {
			this.url = url;
		}

		public String getPicurl() {
			return picurl;
		}

		public void setPicurl(String picurl) {
			this.picurl = picurl;
		}
	}

	/**
	 * 发送图文消息（点击跳转到外链）
	 * 
	 * <pre>
	 * 图文消息条数限制在8条以内，注意，如果图文数超过8，则将会无响应。
	 * </pre>
	 * 
	 * @param touser
	 *            open_id
	 * @param mediaId
	 *            发送的图片/语音/视频/图文消息（点击跳转到图文消息页）的媒体ID
	 * @return ApiResponse
	 */
	public static ApiResponse sendNews(String touser, List<Articles> list) {
		ApiRequest request = new ApiRequest();
		request.set("touser", touser);
		request.set("msgtype", "news");
		request.set("news", ApiRequest.create("articles", list));
		return sendPost(sendMessageUrl + AccessTokenConfig.getAccessToken(),
				request);
	}

	/**
	 * 发送卡券
	 * 
	 * 
	 * @param touser
	 *            open_id
	 * @param mediaId
	 *            发送的图片/语音/视频/图文消息（点击跳转到图文消息页）的媒体ID
	 * @return ApiResponse
	 */
	public static ApiResponse sendWxcard(String touser, String cardId) {
		ApiRequest request = new ApiRequest();
		request.set("touser", touser);
		request.set("msgtype", "wxcard");
		request.set("wxcard", ApiRequest.create("card_id", cardId));
		return sendPost(sendMessageUrl + AccessTokenConfig.getAccessToken(),
				request);
	}
}
